home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / sys / amiga / programmer / 7680 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: news.kempele.fi!lyyra!quaid
  2. From: quaid@lyyra.kempele.fi (Jarkko Vatjus-Anttila)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: Randomness
  5. Date: 20 Apr 1996 15:19:54 GMT
  6. Organization: Kempeleen lukio
  7. Message-ID: <4lavaq$i30@louhi.kempele.fi>
  8. References: <451.6681T677T994@summat.demon.co.uk>
  9. NNTP-Posting-Host: lyyra.kempele.fi
  10. NNTP-Posting-User: quaid
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. Mike Dodd (mike@summat.demon.co.uk) wrote:
  14. : Help !,
  15.  
  16. : Has anyone got a good algorithm for quickly calculating a "random" number ?
  17.  
  18. Well I have. You need a routine to create a seed for rnd numbers and that 
  19. can be easily done *for example* with this routine.
  20.  
  21.  move.l #datestamp,d1   ; space for DateStamp() output. 12 bytes.
  22.  move.l dosbase,a6
  23.  jsr    _LVODateStamp(a6)
  24.  move.l d0,a0
  25.  move.l 4(a0),d1
  26.  mulu.w d1,d0
  27.  move.l 4(a0),d1        ; number of tics (1/50th seconds)
  28.  mulu.w d1,d0
  29.  move.w d0,seed         ; this is the seed.
  30.  
  31. The seed has to be calculated only when you start the program. This 
  32. routine uses DateStamp routine to get system time and because it uses the 
  33. number of tics to create the seed, it's hardly never the same. This gives 
  34. you totally random numbers.
  35.  
  36. Once the seed is initialised you can yse this routine to create rnd numbers:
  37. Just give the max limit in d0.
  38.  
  39. rndnumber
  40.  move.w seed,d1
  41.  mulu.w #31421,d1
  42.  add.w  #6927,d1
  43.  mulu.w d1,d0
  44.  move.w d1,seed  ; init new seed to make sure that numbers remain random 
  45.  clr.w  d0
  46.  swap   d0       ; upper 16 bits are the most random.
  47.  rts
  48.  
  49. : I've tried the algorithm described in the autodocs for amiga.lib fo
  50. : "FastRand" :
  51.  
  52. I once tried to deal with those FastRand and other routines but with no 
  53. success. I think they suck. I don't know if I did somethign wrong when I 
  54. tried to create numbers with 'em, but actually I don't care. My routine 
  55. is capable to create rnd numbers in range 0-65536 and that sure is enough 
  56. to me. The main thing is that I get rnd numbers.
  57.  
  58. : but this produces a rather poor sequence of values...Soooerh, any better
  59. : suggestions ?
  60.  
  61. What do you think of mine?
  62.  
  63. --
  64.     --------------------------------------------------------------
  65.     |AMiGA      Jarkko Vatjus-Anttila (Quaid/BFB Team)      POWER|
  66.     |WWW: http://www.kempele.fi/~quaid/   EMail: quaid@kempele.fi|
  67.     --------------------------------------------------------------
  68.       |_|  !A1200/030/50Mhz/882/50Mhz/6mb memory/840MB HDD!  |_|
  69.